package greendroid.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * This is an abstract view, It's extends by MoreView.
 */
public abstract class AbstractView {
    protected static final Handler mHandler = new Handler();
    private ViewGroup mParent;
    private boolean mIsEnriched = false;
    private Activity mActivity;
    private LayoutInflater mInflater;
    public AbstractView(Activity activity) {
        mActivity = activity;
        mInflater = LayoutInflater.from(activity);
        final ViewGroup parent = getParent();
        if (null == parent) {
            throw new NullPointerException(
                    "getParent() must return a non-null ViewGroup.");
        }
        parent.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT));
        mParent = parent;
    }
    public abstract void enrichContent(ViewGroup parent);
    protected ViewGroup getParent() {
        return new RelativeLayout(getActivity());
    }

    public final View getContentView() {
        return mParent;
    }
    
    /**
     * If view is not initialized, and then initialize it.
     */
    public final void initializeIfNecessary() {
        if (mIsEnriched) {
            return;
        }
        enrichContent(mParent);
        onFinishInit();
        mIsEnriched = true;
    }

    /**
     * Called after you have enriched the view tree, this is the best place
     * where you may send normal http request.
     */
    protected void onFinishInit() {
    }
    public static final int FILL_PARENT = ViewGroup.LayoutParams.FILL_PARENT,
    WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
    
    protected void onViewOut() {}
    protected void onViewIn() {}
    protected void onResume() {}
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {}
    
    public View findViewById(int id) {
        return mParent.findViewById(id);
    }
    
    public View inflate(int resource) {
        return mInflater.inflate(resource, null);
    }
    
    public Activity getActivity() {
        return mActivity;
    }
}

